/* Copyright (C) 2004 Stefan Bellon <sbellon@sbellon.de>
 *
 * This file is part of RemotePrinterFS.
 *
 * RemotePrinterFS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * RemotePrinterFS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include "connection.h"
#include "conn_jetdirect.h"
#include "conn_lpd.h"
#include "conn_spool.h"
#include "errors.h"
#include "syslogf.h"
#include "utils.h"

typedef char *(*get_spool_filename_connection)(char *, int);
typedef int (*open_connection)(char *, int, int);
typedef int (*writeto_connection)(int, char *, int);
typedef int (*close_connection)(int);

typedef struct
{
    char                           *name;
    get_spool_filename_connection  get_spool_filename;
    open_connection                open;
    writeto_connection             write;
    close_connection               close;
} proto;

proto protocols[] =
{
    /* HP JetDirect protocol */
    {
        "JetDirect",
        jetdirect_get_spool_filename,
        jetdirect_open,
        jetdirect_write,
        jetdirect_close
    },
    /* Line Printer Daemon Protocol */
    {
        "LPD",
        lpd_get_spool_filename,
        lpd_open,
        lpd_write,
        lpd_close
    },
    /* Sentinel, DO NOT REMOVE! */
    {
        NULL,
        NULL,
        NULL,
        NULL,
        NULL
    }
};

static get_spool_filename_connection proto_get_spool_filename = NULL;
static open_connection               proto_open               = NULL;
static writeto_connection            proto_write              = NULL;
static close_connection              proto_close              = NULL;
static int                           count                    = 0;

static os_error const *
match_on_proto(char *key, char *value)
{
    if (!strcasecmp(key, "proto")) {
        int i = 0;
        while (protocols[i].name) {
            if (!strcasecmp(value, protocols[i].name)) {
                proto_get_spool_filename = protocols[i].get_spool_filename;
                proto_open               = protocols[i].open;
                proto_write              = protocols[i].write;
                proto_close              = protocols[i].close;
                return NULL;
            }
            ++i;
        }
        syslogf(LOG_ERR, "Invalid value for key '%s': %s", key, value);
        return ERR(err_InvalidSpecialField);
    }
    return NULL;
}

os_error const *
detect_protocol(char *special_field, int length, bool may_spool)
{
    char *spool_filename;
    
    error = parse_special_field(special_field, match_on_proto);
    if (error)
        return error;

    if (!proto_open || !proto_write || !proto_close ||
        !proto_get_spool_filename)
    {
        syslogf(LOG_CRIT, "Internal error: proto_* == NULL");
        return ERR(err_InternalError);
    }

    if (may_spool) {
        spool_filename = proto_get_spool_filename(special_field, length);
        if (spool_filename) {
            set_spool_filename(spool_filename);
            proto_get_spool_filename = NULL;
            proto_open               = spool_open;
            proto_write              = spool_write;
            proto_close              = spool_close;
        }
    }

    return NULL;
}

int
connection_open(char *special_field, int length, int filetype)
{
    if (!proto_open) {
        syslogf(LOG_CRIT, "Internal error: proto_open == NULL");
        error = ERR(err_InternalError);
        return -1;
    }

    syslogf(LOG_DEBUG|LOG_INFO, "Opening connection ...");
    count = 0;

    return proto_open(special_field, length, filetype);
}

int
connection_write(int s, char *buf, int len)
{
    if (!proto_write) {
        syslogf(LOG_CRIT, "Internal error: proto_write == NULL");
        error = ERR(err_InternalError);
        return -1;
    }

    if (len < 0) {
        syslogf(LOG_ERR,
                "connection_write: about to write %lli bytes to connection",
                len);
        error = ERR(err_InternalError);
        return -1;
    }

    syslogf(LOG_DEBUG|LOG_INFO|LOG_NOTICE,
            "Writing %i bytes to connection ...", len);
    count += len;

    return proto_write(s, buf, len);
}

int
connection_close(int s)
{
    if (!proto_close) {
        syslogf(LOG_CRIT, "Internal error: proto_close == NULL");
        error = ERR(err_InternalError);
        return -1;
    }

    syslogf(LOG_DEBUG, "%i bytes of data sent.", count);
    
    return proto_close(s);
}
